package tabuvrp.gui;

import tabuvrp.core.stage.Stage;
import tabuvrp.tabustage.TabuStage;
import tabuvrp.tabustage.TabuStageListener;


public class TabuStageDisplay extends javax.swing.JPanel implements TabuStageListener {

    /** Creates new form TabuStageDisplay */
    public TabuStageDisplay() {
        initComponents();
        clear();
    }

    public void clear() {
        improvements = 0;
        stepsField.setText("-");
        usefulField.setText("-");
        feasibleField.setText("-");
        infeasibleField.setText("-");
        elapsedField.setText("-");
        solutionImprovementsField.setText("-");
        initialCostField.setText("-");
        finalCostField.setText("-");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        stepsLabel = new javax.swing.JLabel();
        stepsField = new javax.swing.JTextField();
        elapsedLabel = new javax.swing.JLabel();
        elapsedField = new javax.swing.JTextField();
        feasibleLabel = new javax.swing.JLabel();
        infeasibleLabel = new javax.swing.JLabel();
        infeasibleField = new javax.swing.JTextField();
        feasibleField = new javax.swing.JTextField();
        usefulLabel = new javax.swing.JLabel();
        usefulField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        initialCostLabel = new javax.swing.JLabel();
        initialCostField = new javax.swing.JTextField();
        finalCostLabel = new javax.swing.JLabel();
        finalCostField = new javax.swing.JTextField();
        solutionImprovementsLabel = new javax.swing.JLabel();
        solutionImprovementsField = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(385, 322));

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        stepsLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        stepsLabel.setText("Steps");

        stepsField.setEditable(false);
        stepsField.setFocusable(false);
        stepsField.setPreferredSize(new java.awt.Dimension(64, 19));

        elapsedLabel.setText("Elapsed time");

        elapsedField.setEditable(false);
        elapsedField.setFocusable(false);
        elapsedField.setPreferredSize(new java.awt.Dimension(64, 19));

        feasibleLabel.setForeground(new java.awt.Color(0, 102, 0));
        feasibleLabel.setText("Feasible");

        infeasibleLabel.setForeground(new java.awt.Color(102, 0, 0));
        infeasibleLabel.setText("Not feasible");

        infeasibleField.setEditable(false);
        infeasibleField.setFocusable(false);
        infeasibleField.setPreferredSize(new java.awt.Dimension(64, 19));

        feasibleField.setEditable(false);
        feasibleField.setFocusable(false);
        feasibleField.setPreferredSize(new java.awt.Dimension(64, 19));

        usefulLabel.setText("Useful");

        usefulField.setEditable(false);
        usefulField.setFocusable(false);
        usefulField.setPreferredSize(new java.awt.Dimension(64, 19));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(stepsLabel)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usefulLabel)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(infeasibleLabel)
                                    .addComponent(feasibleLabel))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(infeasibleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(feasibleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(usefulField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(stepsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(59, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(78, Short.MAX_VALUE)
                .addComponent(elapsedLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(elapsedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stepsLabel)
                    .addComponent(stepsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usefulLabel)
                    .addComponent(usefulField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(feasibleLabel)
                    .addComponent(feasibleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(infeasibleLabel)
                    .addComponent(infeasibleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(elapsedLabel)
                    .addComponent(elapsedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        initialCostLabel.setText("Initial Cost");

        initialCostField.setEditable(false);
        initialCostField.setFocusable(false);
        initialCostField.setPreferredSize(new java.awt.Dimension(64, 19));

        finalCostLabel.setText("Final Cost");

        finalCostField.setEditable(false);
        finalCostField.setFocusable(false);
        finalCostField.setPreferredSize(new java.awt.Dimension(64, 19));

        solutionImprovementsLabel.setText("Solution improvements");

        solutionImprovementsField.setEditable(false);
        solutionImprovementsField.setFocusable(false);
        solutionImprovementsField.setPreferredSize(new java.awt.Dimension(64, 19));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(solutionImprovementsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(solutionImprovementsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(83, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(finalCostLabel)
                    .addComponent(initialCostLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(finalCostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(initialCostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(solutionImprovementsLabel)
                    .addComponent(solutionImprovementsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(initialCostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(initialCostLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(finalCostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(finalCostLabel))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField elapsedField;
    private javax.swing.JLabel elapsedLabel;
    private javax.swing.JTextField feasibleField;
    private javax.swing.JLabel feasibleLabel;
    private javax.swing.JTextField finalCostField;
    private javax.swing.JLabel finalCostLabel;
    private javax.swing.JTextField infeasibleField;
    private javax.swing.JLabel infeasibleLabel;
    private javax.swing.JTextField initialCostField;
    private javax.swing.JLabel initialCostLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField solutionImprovementsField;
    private javax.swing.JLabel solutionImprovementsLabel;
    private javax.swing.JTextField stepsField;
    private javax.swing.JLabel stepsLabel;
    private javax.swing.JTextField usefulField;
    private javax.swing.JLabel usefulLabel;
    // End of variables declaration//GEN-END:variables
    private int improvements;

    public void stageStarted(Stage stage) {
        initialCostField.setText(String.valueOf(stage.getBestSolution().getCost()));
    }

    public void stepDone(Stage stage) {
        stepsField.setText(String.valueOf(stage.getSteps()));
        elapsedField.setText(String.valueOf(stage.getElaborationTime() * 10E-10));
    }

    public void newBestSolution(Stage stage) {
        improvements += 1;
        solutionImprovementsField.setText(String.valueOf(improvements));
    }

    public void stageStopped(Stage stage) {
        finalCostField.setText(String.valueOf(stage.getBestSolution().getCost()));
    }

    public void newUsefulStep(TabuStage stage, boolean feasible) {
        usefulField.setText(String.valueOf(stage.getUsefulSteps()));
        if (feasible) {
            feasibleField.setText(String.valueOf(stage.getFeasSteps()));
        }
        else {
            infeasibleField.setText(String.valueOf(stage.getInfeasSteps()));
        }
    }
}
